package org.bangumibuddy.parser.impl;

import com.dgtlrepublic.anitomyj.AnitomyJ;
import com.dgtlrepublic.anitomyj.Element;
import org.bangumibuddy.parser.AnimeMetaParser;
import org.bangumibuddy.pojo.anime.AnimeMetaData;

import java.util.List;

public class DefaultAnimeMetaParser implements AnimeMetaParser {

    List<Element> elements;

    @Override
    public AnimeMetaData parse(String filename) {
        InternalParse(filename);
        AnimeMetaData animeMetaData = new AnimeMetaData();
        animeMetaData.setEpisode(Integer.parseInt(getValue(Element.ElementCategory.kElementEpisodeNumber)));
        animeMetaData.setGroup(getValue(Element.ElementCategory.kElementReleaseGroup));
        animeMetaData.setSeason(Integer.parseInt(getValue(Element.ElementCategory.kElementAnimeSeason) == "" ? "1" : getValue(Element.ElementCategory.kElementAnimeSeason)));
        //TODO 添加对不同语言的支持
        animeMetaData.setTitle_en(getValue(Element.ElementCategory.kElementAnimeTitle));
        animeMetaData.setSource(getValue(Element.ElementCategory.kElementSource));

        return animeMetaData;
    }

    private void InternalParse(String filename){
        elements = AnitomyJ.parse(filename);
    }

    private String getValue(Element.ElementCategory key) {
        for (Element element : elements) {
            if (element.getCategory() == key) {
                return element.getValue();
            }
        }

        return "";
    }
}
